package com.borko.healthcare.tapestry.pages;

import java.util.List;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Cookies;
import org.springframework.context.ApplicationContext;

import com.borko.healthcare.entities.interfaces.Medicine;
import com.borko.healthcare.entities.interfaces.MedicineOrder;
import com.borko.healthcare.entities.interfaces.Notification;
import com.borko.healthcare.entities.interfaces.Pharmacist;
import com.borko.healthcare.entities.interfaces.Supplier;
import com.borko.healthcare.persistence.hibernate.services.MedicineManager;
import com.borko.healthcare.persistence.hibernate.services.MedicineOrderManager;
import com.borko.healthcare.persistence.hibernate.services.NotificationManager;
import com.borko.healthcare.persistence.hibernate.services.PharmacistManager;
import com.borko.healthcare.persistence.hibernate.services.SupplierManager;

public class ViewNotification {
	
	@Inject
	private Cookies cookies;
	
	private Notification notification;
	
	@SuppressWarnings("unused")
	@Property
	private MedicineOrder medicineOrder;
	
	@Inject
	private ApplicationContext applicationContext;
	
	private PharmacistManager pharmacistManager;
	private MedicineOrderManager medicineOrderManager;
	private NotificationManager notificationManager;
	private MedicineManager medicineManager;
	private SupplierManager supplierManager;
	
	public List<MedicineOrder> getMedicineOrders() {
		return getNotification().getMedicineOrders();
	}
	
	public String getSupplierName() {
		return notification.getSupplier().getUserDetails().getFullName();
	}
	
	public String getPharmacistName() {
		return notification.getPharmacist().getUserDetails().getFullName();
	}
	
	public void onActionFromAccept(int medicineOrderId) {
		medicineOrderManager = (MedicineOrderManager) applicationContext.getBean("medicineOrderManager");
		MedicineOrder medicineOrder = medicineOrderManager.getMedicineOrder(medicineOrderId);
		medicineOrder.setAccepted(true);
		medicineOrderManager.applyChanges(medicineOrder); // Update medicine order
		getNotification().getPharmacist().getMedicines().add(medicineOrder.getMedicine());
		
		pharmacistManager = (PharmacistManager) applicationContext.getBean("pharmacistManager");
		Pharmacist pharmacist = notification.getPharmacist();
		pharmacistManager.applyChanges(pharmacist); // update pharmacist
		
		medicineManager = (MedicineManager) applicationContext.getBean("medicineManager");
		medicineOrder.getMedicine().setPharmacist(notification.getPharmacist());
		Medicine medicine = medicineOrder.getMedicine();
		medicineManager.applyChanges(medicine); // update medicine
	}
	
	public void onActionFromReject(int medicineOrderId) {
		medicineOrderManager = (MedicineOrderManager) applicationContext.getBean("medicineOrderManager");
		MedicineOrder medicineOrder = medicineOrderManager.getMedicineOrder(medicineOrderId);
		medicineOrder.setAccepted(false);
		medicineOrderManager.add(medicineOrder); // Update medicine order
	}

	public Notification getNotification() {
		notificationManager = (NotificationManager) applicationContext.getBean("notificationManager");
		List<Notification> notifications = notificationManager.list();
		
		supplierManager = (SupplierManager) applicationContext.getBean("supplierManager");
		Supplier supplier = supplierManager.getSupplier(Integer.parseInt(cookies.readCookieValue("userId")));
		for (Notification notification : notifications) {
			if (notification.getSupplier().getUserDetails().getEmail().equals(supplier.getUserDetails().getEmail())) {
				this.notification = notification;
				break;
			}
		}
		return this.notification;
	}

	public void setNotification(Notification notification) {
		this.notification = notification;
	}
	
	
}
